home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / dviware / dvitovdu32 / src / pascal / fontreader.p < prev    next >
Text File  |  1991-11-10  |  30KB  |  898 lines

  1. (* FontReader implements the routines for reading character metric and
  2.    bitmap information from PK files, or from TFM files for PostScript fonts.
  3.    The metric information accessed by PixelTableRoutine is used by
  4.    DVIReader to calculate character positions on a page.
  5.    The bitmap information accessed by GetBitmap is used by the
  6.    main program to display characters (from non-PostScript fonts).
  7. *)
  8.  
  9. #include 'globals.h';
  10. #include 'files.h';
  11. #include 'screenio.h';
  12. #include 'vdu.h';
  13. #include 'options.h';
  14. #include 'dvireader.h';
  15. #include 'fontreader.h';
  16.  
  17. VAR
  18.    PTfile : integer;                   (* PK/TFM file descriptor              *)
  19.    PToffset : INTEGER;                 (* current byte offset in PTfile       *)
  20.    currPTbuff : INTEGER;               (* starting byte offset in buffer      *)
  21.    PTbuffer : buffer;                  (* input buffer                        *)
  22.    psprefixlen,                        (* length of psprefix string           *)
  23.    fontdirlen : INTEGER;               (* length of fontdir string            *)
  24.    gpower : ARRAY [0..32] OF BITSET;   (* 0,1,11,111,1111,...                 *)
  25.    turnon : BOOLEAN;                   (* is current run black?               *)
  26.    dynf,                               (* dynamic packing variable            *)
  27.    repeatcount,                        (* times to repeat the next row        *)
  28.    bitweight : INTEGER;                (* for bits or nybbles from inputbyte  *)
  29.    inputbyte : bytes_or_bits;          (* the current input byte              *)
  30.    lf, lh, bc, ec, nw, nh : INTEGER;   (* TFM file data                       *)
  31.    TFMinfo     : ARRAY [0..255] OF
  32.                     RECORD
  33.                        wdindex, htindex, dpindex : INTEGER;
  34.                     END;
  35.    charmetrics : ARRAY [0..255] OF
  36.                     RECORD
  37.                        width, height, depth : ARRAY [0..3] OF INTEGER;
  38.                     END;
  39.  
  40. (******************************************************************************)
  41.  
  42. PROCEDURE BuildTFMSpec (fontptr : fontinfoptr);
  43.  
  44. (* Build a complete TFM file specification in fontptr^.fontspec.
  45.    This will only be done once per font; fontspeclen will no longer be 0.
  46.    fontptr^.fontexists becomes TRUE if the file can be opened.
  47. *)
  48.  
  49. LABEL 999;
  50.  
  51. VAR f, result, i, nxt : INTEGER;
  52.  
  53. BEGIN
  54. WITH fontptr^ DO BEGIN
  55.    i := 0;
  56.    IF fontarealen > 0 THEN BEGIN
  57.       nxt := fontarealen;
  58.       REPEAT
  59.          fontspec[i] := fontarea[i];   (* start fontspec with fontarea *)
  60.          i := i + 1;
  61.       UNTIL (i = nxt) OR (i > maxfontspec);
  62.    END
  63.    ELSE BEGIN
  64.       nxt := Len(tfmdir);              (* assume > 0 *)
  65.       REPEAT
  66.          fontspec[i] := tfmdir[i];     (* start fontspec with tfmdir *)
  67.          i := i + 1;
  68.       UNTIL (i = nxt) OR (i > maxfontspec);
  69.    END;
  70.    IF nxt >= maxfontspec THEN BEGIN
  71.       fontspeclen := maxfontspec;
  72.       goto 999;                        (* fontspec truncated *)
  73.    END;
  74.    (* nxt is current length of fontspec; append fontname.tfm *)
  75.    i := 0;
  76.    WHILE (i < fontnamelen) AND (nxt < maxfontspec) DO BEGIN
  77.       fontspec[nxt] := fontname[i];    (* append fontname *)
  78.       i := i + 1;
  79.       nxt := nxt + 1;
  80.    END;
  81.    IF nxt + 4 <= maxfontspec THEN BEGIN      (* append .tfm *)
  82.       fontspec[nxt] := '.'; nxt := nxt + 1;
  83.       fontspec[nxt] := 't'; nxt := nxt + 1;
  84.       fontspec[nxt] := 'f'; nxt := nxt + 1;
  85.       fontspec[nxt] := 'm'; nxt := nxt + 1;
  86.    END
  87.    ELSE BEGIN
  88.       fontspeclen := maxfontspec;
  89.       goto 999;                        (* fontspec truncated *)
  90.    END;
  91.    fontspeclen := nxt;
  92.    IF fontspeclen < maxstring THEN fontspec[fontspeclen] := CHR(0);
  93.    f := open(fontspec,O_RDONLY,0);     (* try to open file *)
  94.    IF fontspeclen < maxstring THEN fontspec[fontspeclen] := ' ';
  95.    IF f >= 0 THEN BEGIN
  96.       result := close(f);
  97.       fontexists := TRUE;              (* fontspec exists *)
  98.    END;
  99. END;
  100. 999:
  101. END; (* BuildTFMSpec *)
  102.  
  103. (******************************************************************************)
  104.  
  105. FUNCTION CompleteFontSpec (fontptr : fontinfoptr;
  106.                            nxt : INTEGER;
  107.                            fontsizelen : INTEGER;
  108.                            VAR firstn : INTEGER) : BOOLEAN;
  109.  
  110. (* Return TRUE if we can append "fontname.n...npk" to fontspec.
  111.    Such a scheme is used in the latest TeX distributions.
  112. *)
  113.  
  114. LABEL 999;
  115.  
  116. VAR i : INTEGER;
  117.  
  118. BEGIN
  119. WITH fontptr^ DO BEGIN
  120.    i := 0;
  121.    WHILE (i < fontnamelen) AND (nxt < maxfontspec) DO BEGIN
  122.       fontspec[nxt] := fontname[i];                    (* append fontname *)
  123.       i := i + 1;
  124.       nxt := nxt + 1;
  125.    END;
  126.    firstn := nxt + 1;                                  (* position of 1st n *)
  127.    IF nxt + fontsizelen + 2 < maxfontspec THEN BEGIN
  128.       fontspec[nxt] := '.';
  129.       nxt := nxt + fontsizelen + 1;                    (* skip n...n *)
  130.       fontspec[nxt] := 'p';                            (* append pk *)
  131.       nxt := nxt + 1;
  132.       fontspec[nxt] := 'k';
  133.       nxt := nxt + 1;
  134.    END
  135.    ELSE BEGIN
  136.       fontspeclen := maxfontspec;
  137.       CompleteFontSpec := FALSE;
  138.       goto 999;                                        (* fontspec truncated *)
  139.    END;
  140.    fontspeclen := nxt;
  141.    IF nxt < maxfontspec THEN fontspec[nxt] := ' ';     (* terminate string *)
  142.    CompleteFontSpec := TRUE;
  143. END;
  144. 999:
  145. END; (* CompleteFontSpec *)
  146.  
  147. (******************************************************************************)
  148.  
  149. PROCEDURE BuildFontSpec (fontptr : fontinfoptr);
  150.  
  151. (* Build a complete file specification in fontptr^.fontspec.
  152.    This will only be done once per font; fontspeclen will no longer be 0.
  153.    fontptr^.fontexists becomes TRUE if the file can be opened.
  154. *)
  155.  
  156. LABEL 888, 999;
  157.  
  158. VAR
  159.    f, result, i, j, nxt, fontsize,
  160.    firstn, lastn, tempsize, tempsizelen : INTEGER;
  161.  
  162. BEGIN
  163. WITH fontptr^ DO BEGIN
  164.    (* first check for a PostScript font; following code will set psfont to TRUE
  165.       if psprefixlen = 0 --- ALL fonts will be considered PostScript fonts
  166.    *)
  167.    psfont := TRUE;
  168.    i := 0;
  169.    WHILE TRUE DO BEGIN
  170.       IF i = psprefixlen THEN goto 888;
  171.       IF Cap(fontname[i]) <> Cap(psprefix[i]) THEN BEGIN
  172.          psfont := FALSE;
  173.          goto 888;
  174.       END;
  175.       i := i + 1;
  176.    END;
  177.    888:
  178.    IF psfont THEN BEGIN
  179.       BuildTFMSpec(fontptr);           (* build TFM file spec *)
  180.       goto 999;
  181.    END;
  182.    i := 0;
  183.    nxt := fontdirlen;
  184.    REPEAT
  185.       fontspec[i] := fontdir[i];       (* start fontspec with fontdir *)
  186.       i := i + 1;
  187.    UNTIL (i = nxt) OR (i > maxfontspec);
  188.    IF nxt >= maxfontspec THEN BEGIN
  189.       fontspeclen := maxfontspec;
  190.       goto 999;                        (* fontspec truncated *)
  191.    END;
  192.    fontsize := TRUNC( mag * (scaledsize / designsize)
  193.                           * (resolution / 1000.0) + 0.5 );
  194.    IF fontsize = 0 THEN
  195.       fontsize := fontsize + 1;        (* allow for subtracting 1 *)
  196.    tempsize := fontsize;
  197.    i := 1;
  198.    WHILE TRUE DO BEGIN
  199.       (* Complete rest of fontspec starting at nxt
  200.          and return the position of first digit for fontsize.
  201.          We have to try fontsize +/- 1 before giving up because
  202.          rounding problems can occur in the above fontsize calculation.
  203.       *)
  204.       j := tempsize;
  205.       tempsizelen := 0;
  206.       WHILE j > 0 DO BEGIN
  207.          tempsizelen := tempsizelen + 1;
  208.          j := j DIV 10;
  209.       END;
  210.       IF NOT CompleteFontSpec(fontptr, nxt, tempsizelen, firstn) THEN
  211.          goto 999;                     (* fontspec truncated *)
  212.       lastn := firstn + tempsizelen - 1;
  213.       (* put tempsize into fontspec[firstn..lastn] *)
  214.       FOR j := lastn DOWNTO firstn DO BEGIN
  215.          fontspec[j] := CHR(ORD('0') + (tempsize MOD 10));
  216.          tempsize := tempsize DIV 10;
  217.       END;
  218.       IF i > 3 THEN                    (* original fontsize has been restored *)
  219.          goto 999;                     (* could not open fontspec *)
  220.       IF fontspeclen < maxstring THEN fontspec[fontspeclen] := CHR(0);
  221.       f := open(fontspec,O_RDONLY,0);  (* try to open file *)
  222.       IF fontspeclen < maxstring THEN fontspec[fontspeclen] := ' ';
  223.       IF f >= 0 THEN BEGIN
  224.          result := close(f);
  225.          fontexists := TRUE;           (* fontspec exists *)
  226.          goto 999;
  227.       END
  228.       ELSE IF i = 1 THEN
  229.          tempsize := fontsize - 1      (* try fontsize-1 *)
  230.       ELSE IF i = 2 THEN
  231.          tempsize := fontsize + 1      (* try fontsize+1 *)
  232.       ELSE
  233.          tempsize := fontsize;         (* restore original fontsize *)
  234.       i := i + 1;
  235.    END;
  236. END;
  237. 999:
  238. END; (* BuildFontSpec *)
  239.  
  240. (******************************************************************************)
  241.  
  242. FUNCTION  OpenFontFile (VAR name : string) : BOOLEAN;
  243.  
  244. (* Return TRUE if given file can be opened.
  245.    Only one font file will be open at any given time.
  246. *)
  247.  
  248. LABEL 888;
  249.  
  250. VAR length : integer;
  251.  
  252. BEGIN
  253. currPTbuff := -1;   (* impossible value for first GetPTByte *)
  254. length := 0;
  255. WHILE length < maxstring DO BEGIN
  256.    IF name[length] = ' ' THEN goto 888;
  257.    length := length + 1;
  258. END;
  259. 888:
  260. IF length < maxstring THEN name[length] := CHR(0);   (* terminate with NULL *)
  261. PTfile := open(name, O_RDONLY, 0);
  262. IF length < maxstring THEN name[length] := ' ';      (* restore space *)
  263. OpenFontFile := PTfile >= 0;
  264. END; (* OpenFontFile *)
  265.  
  266. (******************************************************************************)
  267.  
  268. PROCEDURE CloseFontFile;
  269.  
  270. (* Close the currently open font file. *)
  271.  
  272. VAR result : integer;
  273.  
  274. BEGIN
  275. result := close(PTfile);
  276. END; (* CloseFontFile *)
  277.  
  278. (******************************************************************************)
  279.  
  280. FUNCTION GetPTByte : INTEGER;
  281.  
  282. (* Returns the value (unsigned) of the byte at PToffset and
  283.    advances PToffset for the next GetPTByte.
  284. *)
  285.  
  286. VAR buffstart, result : INTEGER;
  287.  
  288. BEGIN
  289. buffstart := (PToffset DIV bufflen) * bufflen;   (* 0, bufflen, 2*bufflen... *)
  290. IF buffstart <> currPTbuff THEN BEGIN
  291.    currPTbuff := buffstart;
  292.    result := lseek(PTfile, buffstart, 0);
  293.    { DEBUG
  294.      IF result <> buffstart THEN BEGIN
  295.         writeln('Lseek failed in GetPTByte!'); RestoreTerminal; exit(1);
  296.      END;
  297.    GUBED }
  298.    result := read(PTfile, PTbuffer, bufflen);
  299.    { DEBUG
  300.      IF result = -1 THEN BEGIN
  301.         writeln('Read failed in GetPTByte!'); RestoreTerminal; exit(1);
  302.      END;
  303.    GUBED }
  304. END;
  305. GetPTByte := ORD(PTbuffer[PToffset - buffstart]);
  306. PToffset := PToffset + 1;
  307. END; (* GetPTByte *)
  308.  
  309. (******************************************************************************)
  310.  
  311. FUNCTION SignedPTByte : INTEGER;        (* the next byte, signed *)
  312.  
  313. VAR b : INTEGER;
  314.  
  315. BEGIN
  316. b := GetPTByte;
  317. IF b < 128 THEN
  318.    SignedPTByte := b
  319. ELSE
  320.    SignedPTByte := b - 256;
  321. END; (* SignedPTByte *)
  322.  
  323. (******************************************************************************)
  324.  
  325. FUNCTION GetTwoPTBytes : INTEGER;       (* the next 2 bytes, unsigned *)
  326.  
  327. VAR a, b : INTEGER;
  328.  
  329. BEGIN
  330. a := GetPTByte;
  331. b := GetPTByte;
  332. GetTwoPTBytes := a * 256 + b;
  333. END; (* GetTwoPTBytes *)
  334.  
  335. (******************************************************************************)
  336.  
  337. FUNCTION SignedPTPair : INTEGER;        (* the next 2 bytes, signed *)
  338.  
  339. VAR a, b : INTEGER;
  340.  
  341. BEGIN
  342. a := GetPTByte;
  343. b := GetPTByte;
  344. IF a < 128 THEN
  345.    SignedPTPair := a * 256 + b
  346. ELSE
  347.    SignedPTPair := (a - 256) * 256 + b;
  348. END; (* SignedPTPair *)
  349.  
  350. (******************************************************************************)
  351.  
  352. FUNCTION GetThreePTBytes : INTEGER;     (* the next 3 bytes, unsigned *)
  353.  
  354. VAR a, b, c : INTEGER;
  355.  
  356. BEGIN
  357. a := GetPTByte;
  358. b := GetPTByte;
  359. c := GetPTByte;
  360. GetThreePTBytes := (a * 256 + b) * 256 + c;
  361. END; (* GetThreePTBytes *)
  362.  
  363. (******************************************************************************)
  364.  
  365. FUNCTION SignedPTQuad : INTEGER;        (* the next 4 bytes, signed *)
  366.  
  367. TYPE int_or_bytes = RECORD
  368.                     CASE b : BOOLEAN OF
  369.                        TRUE  : (int : INTEGER);
  370.                        FALSE : (byt : PACKED ARRAY [0..3] OF CHAR);
  371.                     END;
  372.  
  373. VAR w : int_or_bytes;
  374.  
  375. BEGIN
  376. WITH w DO BEGIN
  377.    w.byt[0] := CHR(GetPTByte);
  378.    w.byt[1] := CHR(GetPTByte);
  379.    w.byt[2] := CHR(GetPTByte);
  380.    w.byt[3] := CHR(GetPTByte);
  381. END;
  382. SignedPTQuad := w.int;
  383. END; (* SignedPTQuad *)
  384.  
  385. (******************************************************************************)
  386.  
  387. FUNCTION GetNyb : INTEGER;
  388.  
  389. (* Return next nybble in PK file. *)
  390.  
  391. BEGIN
  392. IF bitweight = 0 THEN BEGIN
  393.    (* SYSDEP: Pyramid Pascal stores bits 7..0 in the LEFT
  394.       byte of a 4-byte BITSET word. *)
  395.    inputbyte.ch[0] := CHR(GetPTByte);
  396.    bitweight := 16;                         (* for next call of GetNyb *)
  397.    GetNyb := ORD(inputbyte.ch[0]) DIV 16;   (* high nybble *)
  398. END
  399. ELSE BEGIN
  400.    bitweight := 0;                          (* for next call of GetNyb *)
  401.    GetNyb := ORD(inputbyte.ch[0]) MOD 16;   (* low nybble *)
  402. END;
  403. END; (* GetNyb *)
  404.  
  405. (******************************************************************************)
  406.  
  407. FUNCTION PackedNum : INTEGER;
  408.  
  409. (* Return next run count using algorithm given in section 23 of PKtype.
  410.    A possible side-effect is to set the global repeatcount value used
  411.    to duplicate the current row.
  412. *)
  413.  
  414. VAR i, j : INTEGER;
  415.  
  416. BEGIN
  417. i := GetNyb;
  418. IF i = 0 THEN BEGIN
  419.    REPEAT j := GetNyb; i := i + 1 UNTIL j <> 0;
  420.    WHILE i > 0 DO BEGIN j := j * 16 + GetNyb; i := i - 1 END;
  421.    PackedNum := j - 15 + (13 - dynf) * 16 + dynf;
  422. END
  423. ELSE IF i <= dynf THEN
  424.    PackedNum := i
  425. ELSE IF i < 14 THEN
  426.    PackedNum := (i - dynf - 1) * 16 + GetNyb + dynf + 1
  427. ELSE BEGIN
  428.    IF i = 14 THEN
  429.       repeatcount := PackedNum   (* recursive *)
  430.    ELSE
  431.       repeatcount := 1;          (* nybble = 15 *)
  432.    PackedNum := PackedNum;       (* recursive *)
  433. END;
  434. END; (* PackedNum *)
  435.  
  436. (******************************************************************************)
  437.  
  438. PROCEDURE GetBitmap (ht, wd, mapadr : INTEGER;   VAR bitmap : int_or_mptr);
  439.  
  440. (* Allocate space for bitmap and fill it in using information from
  441.    character definition starting at mapadr in currently open PK file.
  442.    Note that the memory used by a loaded bitmap is never deallocated.
  443.    Each bitmap row uses an integral number of words (each 32 bits).
  444.    Byte-aligned rows would use about 35% less memory but
  445.    would increase the processing time needed to display each bitmap.
  446.    It was felt that speed is more important than memory.
  447. *)
  448.  
  449. VAR
  450.    wordptr, rowptr : int_or_bptr;
  451.    i, j, flagbyte,
  452.    wordwidth, wordweight,
  453.    rowsleft, hbit, count, bitmapwords : INTEGER;
  454.    word : BITSET;
  455.    bitmapptr : bitmap_ptr;
  456.  
  457. BEGIN
  458. wordwidth := (wd + 31) DIV 32;         (* words in one row of bitmap *)
  459. bitmapwords := ht * wordwidth;         (* memory required by bitmap *)
  460. { DEBUG
  461. IF bitmapwords > large_size THEN WriteChar(CHR(7));   (* bell *)
  462. GUBED }
  463. IF bitmapwords <= small_size THEN
  464.    NEW(bitmapptr,small)
  465. ELSE IF bitmapwords <= big_size THEN
  466.    NEW(bitmapptr,big)
  467. ELSE IF bitmapwords <= large_size THEN
  468.    NEW(bitmapptr,large)
  469. ELSE IF bitmapwords <= huge_size THEN
  470.    NEW(bitmapptr,huge)
  471. ELSE BEGIN
  472.    WriteString('Character too big!  size=');
  473.    WriteInt(bitmapwords); WriteLine; RestoreTerminal; exit(1);
  474. END;
  475. bitmap.mptr := bitmapptr;              (* return start of bitmap *)
  476. wordptr.int := bitmap.int;
  477. PToffset := mapadr;                    (* mapadr = flagbyte offset in PK file *)
  478. flagbyte := GetPTByte;                 (* assume < 240 *)
  479. dynf := flagbyte DIV 16;               (* dynamic packing variable *)
  480. turnon := (flagbyte MOD 16) >= 8;      (* is 1st pixel black? *)
  481. flagbyte := flagbyte MOD 8;            (* value of bottom 3 bits *)
  482. IF flagbyte < 4 THEN                   (* skip short char preamble *)
  483.    PToffset := PToffset + 10
  484. ELSE IF flagbyte < 7 THEN              (* skip extended short char preamble *)
  485.    PToffset := PToffset + 16
  486. ELSE                                   (* skip long char preamble *)
  487.    PToffset := PToffset + 36;
  488. bitweight := 0;                        (* to get 1st inputbyte *)
  489. IF dynf = 14 THEN BEGIN
  490.    (* raster info is a string of bits in the next (wd * ht + 7) DIV 8 bytes *)
  491.    FOR i := 1 TO ht DO BEGIN
  492.       word := [];                                      (* set all bits to 0 *)
  493.       wordweight := 31;                                (* leftmost bit *)
  494.       FOR j := 1 TO wd DO BEGIN
  495.          IF bitweight = 0 THEN BEGIN
  496.             (* SYSDEP: Pyramid Pascal stores bits 7..0 in the LEFT
  497.                byte of a 4-byte BITSET word. *)
  498.             inputbyte.ch[0] := CHR(GetPTByte);
  499.             bitweight := 8;
  500.          END;
  501.          bitweight := bitweight - 1;                   (* 7..0 *)
  502.          IF bitweight IN inputbyte.bits THEN
  503.             word := word + [wordweight];               (* set bit *)
  504.          IF wordweight > 0 THEN
  505.             wordweight := wordweight - 1
  506.          ELSE BEGIN
  507.             wordptr.bptr^ := word;
  508.             wordptr.int := wordptr.int + 4;
  509.             word := []; wordweight := 31;
  510.          END;
  511.       END;
  512.       IF wordweight < 31 THEN BEGIN
  513.          wordptr.bptr^ := word;
  514.          wordptr.int := wordptr.int + 4;   (* start of next word *)
  515.       END;
  516.    END;
  517. END
  518. ELSE BEGIN
  519.    (* raster info is encoded as run and repeat counts *)
  520.    rowsleft := ht;     hbit := wd;   repeatcount := 0;
  521.    wordweight := 32;   word := [];
  522.    rowptr := wordptr;                (* remember start of row *)
  523.    WHILE rowsleft > 0 DO BEGIN
  524.       count := PackedNum;
  525.       WHILE count > 0 DO BEGIN
  526.          IF (count < wordweight) AND (count < hbit) THEN BEGIN
  527.             IF turnon THEN
  528.                word := word + gpower[wordweight] - gpower[wordweight - count];
  529.             hbit := hbit - count;
  530.             wordweight := wordweight - count;
  531.             count := 0;
  532.          END
  533.          ELSE IF (count >= hbit) AND (hbit <= wordweight) THEN BEGIN
  534.             IF turnon THEN
  535.                word := word + gpower[wordweight] - gpower[wordweight - hbit];
  536.             wordptr.bptr^ := word;
  537.             (* end of current row, so duplicate repeatcount times *)
  538.             FOR i := 1 TO repeatcount DO
  539.                FOR j := 1 TO wordwidth DO BEGIN
  540.                   wordptr.int := wordptr.int + 4;
  541.                   wordptr.bptr^ := rowptr.bptr^;
  542.                   rowptr.int := rowptr.int + 4;
  543.                END;
  544.             rowsleft := rowsleft - (repeatcount + 1);
  545.             repeatcount := 0;
  546.             word := [];
  547.             wordptr.int := wordptr.int + 4;
  548.             rowptr := wordptr;       (* remember start of next row *)
  549.             wordweight := 32;
  550.             count := count - hbit;
  551.             hbit := wd;
  552.          END
  553.          ELSE BEGIN
  554.             IF turnon THEN word := word + gpower[wordweight];
  555.             wordptr.bptr^ := word;
  556.             wordptr.int := wordptr.int + 4;
  557.             word := [];
  558.             count := count - wordweight;
  559.             hbit := hbit - wordweight;
  560.             wordweight := 32;
  561.          END;
  562.       END;
  563.       turnon := NOT turnon;
  564.    END;
  565. END;
  566. END; (* GetBitmap *)
  567.  
  568. (******************************************************************************)
  569.  
  570. FUNCTION FixToDVI (b0, b1, b2, b3 : INTEGER) : INTEGER;
  571.  
  572. (* Convert the given fix width (made up of 4 bytes) into DVI units
  573.    using the method recommended in DVITYPE.
  574. *)
  575.  
  576. VAR alpha, beta, temp : INTEGER;
  577.  
  578. BEGIN
  579. WITH currfont^ DO BEGIN
  580.    alpha := 16 * scaledsize;
  581.    beta  := 16;
  582.    WHILE scaledsize >= 8#40000000 DO BEGIN   (* 2^23 *)
  583.       scaledsize := scaledsize DIV 2;
  584.       beta := beta DIV 2;
  585.    END;
  586.    temp := (((((b3 * scaledsize) DIV 8#400) +
  587.                    (b2 * scaledsize)) DIV 8#400) +
  588.                        (b1 * scaledsize)) DIV beta;
  589.    IF b0 > 0 THEN
  590.       IF b0 = 255 THEN
  591.          FixToDVI := temp - alpha
  592.       ELSE BEGIN
  593.          WriteString('Bad TFM width! 1st byte='); WriteInt(b0);
  594.          WriteLine; RestoreTerminal; exit(1);
  595.       END
  596.    ELSE
  597.       FixToDVI := temp;
  598. END;
  599. END; (* FixToDVI *)
  600.  
  601. (******************************************************************************)
  602.  
  603. PROCEDURE PKFillPixelTable;
  604.  
  605. (* Fill the pixeltable for currfont^ using the font directory info
  606.    in the currently open PK file.
  607. *)
  608.  
  609. LABEL 888;
  610.  
  611. CONST
  612.    pkid   =  89;
  613.    pkpost = 245;
  614.    pknoop = 246;
  615.    pkpre  = 247;
  616.  
  617. VAR
  618.    i, j, flagbyte, flagpos,
  619.    chcode,                       (* assumed to be <= 255 *)
  620.    packetlen, endofpacket,
  621.    b0, b1, b2, b3 : INTEGER;     (* 4 bytes in TFM width *)
  622.  
  623. BEGIN
  624. WITH currfont^ DO BEGIN
  625.    PToffset := 0;                          (* move to first byte *)
  626.    IF GetPTByte <> pkpre THEN BEGIN
  627.       WriteString('Bad pre command in'); WriteChar(' ');
  628.       WriteString(fontspec); WriteLine; RestoreTerminal; exit(1);
  629.    END;
  630.    IF GetPTByte <> pkid THEN BEGIN
  631.       WriteString('Bad id byte in'); WriteChar(' ');
  632.       WriteString(fontspec); WriteLine; RestoreTerminal; exit(1);
  633.    END;
  634.    j := GetPTByte;                         (* length of comment *)
  635.    PToffset := PToffset + j + 16;          (* skip rest of preamble *)
  636.    FOR i := 0 TO maxTeXchar DO
  637.       WITH pixelptr^[i] DO BEGIN
  638.          mapadr := 0;                      (* all chars absent initially *)
  639.          bitmap.mptr := NIL;
  640.       END;
  641.    WHILE TRUE DO BEGIN
  642.       flagpos  := PToffset;                (* remember position of flagbyte *)
  643.       flagbyte := GetPTByte;
  644.       IF flagbyte < 240 THEN BEGIN         (* read character definition *)
  645.          flagbyte := flagbyte MOD 8;       (* value of bottom 3 bits *)
  646.          IF flagbyte < 4 THEN BEGIN        (* short char preamble *)
  647.             packetlen := flagbyte * 256 + GetPTByte;
  648.             chcode    := GetPTByte;
  649.             endofpacket := packetlen + PToffset;
  650.             WITH pixelptr^[chcode] DO BEGIN
  651.                b1     := GetPTByte;
  652.                b2     := GetPTByte;
  653.                b3     := GetPTByte;
  654.                dwidth := FixToDVI(0,b1,b2,b3);     (* b0 = 0 *)
  655.                pwidth := GetPTByte;
  656.                wd     := GetPTByte;
  657.                ht     := GetPTByte;
  658.                xo     := SignedPTByte;
  659.                yo     := SignedPTByte;
  660.             END;
  661.          END
  662.          ELSE IF flagbyte < 7 THEN BEGIN   (* extended short char preamble *)
  663.             packetlen := (flagbyte - 4) * 65536 + GetTwoPTBytes;
  664.             chcode    := GetPTByte;
  665.             endofpacket := packetlen + PToffset;
  666.             WITH pixelptr^[chcode] DO BEGIN
  667.                b1     := GetPTByte;
  668.                b2     := GetPTByte;
  669.                b3     := GetPTByte;
  670.                dwidth := FixToDVI(0,b1,b2,b3);     (* b0 = 0 *)
  671.                pwidth := GetTwoPTBytes;
  672.                wd     := GetTwoPTBytes;
  673.                ht     := GetTwoPTBytes;
  674.                xo     := SignedPTPair;
  675.                yo     := SignedPTPair;
  676.             END;
  677.          END
  678.          ELSE BEGIN                        (* long char preamble *)
  679.             packetlen := SignedPTQuad;
  680.             chcode    := SignedPTQuad;
  681.             endofpacket := packetlen + PToffset;
  682.             WITH pixelptr^[chcode] DO BEGIN
  683.                b0     := GetPTByte;
  684.                b1     := GetPTByte;
  685.                b2     := GetPTByte;
  686.                b3     := GetPTByte;
  687.                dwidth := FixToDVI(b0,b1,b2,b3);
  688.                pwidth := SignedPTQuad DIV 65536;   (* dx in pixels *)
  689.                PToffset := PToffset + 4;           (* skip dy *)
  690.                wd     := SignedPTQuad;
  691.                ht     := SignedPTQuad;
  692.                xo     := SignedPTQuad;
  693.                yo     := SignedPTQuad;
  694.             END;
  695.          END;
  696.          WITH pixelptr^[chcode] DO
  697.             IF (wd = 0) OR (ht = 0) THEN
  698.                mapadr := 0                 (* no bitmap *)
  699.             ELSE
  700.                mapadr := flagpos;          (* position of flagbyte *)
  701.          PToffset := endofpacket;          (* skip raster info *)
  702.       END
  703.       ELSE
  704.          CASE flagbyte OF
  705.             240, 241, 242, 243 :
  706.                        BEGIN
  707.                        i := 0;
  708.                        FOR j := 240 TO flagbyte DO i := 256 * i + GetPTByte;
  709.                        PToffset := PToffset + i;   (* skip special parameter *)
  710.                        END;
  711.             244      : PToffset := PToffset + 4;   (* skip numspecial param *)
  712.             pknoop   : ;                           (* do nothing *)
  713.             pkpost   : goto 888;                   (* no more char defs *)
  714.          OTHERWISE
  715.             WriteString('Bad flag byte in'); WriteChar(' ');
  716.             WriteString(fontspec); WriteLine; RestoreTerminal; exit(1);
  717.          END;
  718.    END; (* of LOOP; flagbyte = pkpost *)
  719.    888:
  720. END;
  721. END; (* PKFillPixelTable *)
  722.  
  723. (******************************************************************************)
  724.  
  725. PROCEDURE ReadTFMIntegers;
  726.  
  727. (* Read the first 6 16-bit integers in the TFM file.  See TFtoPL section 8. *)
  728.  
  729. BEGIN
  730. PToffset := 0;   (* start reading at 1st byte in TFM file *)
  731. lf := GetTwoPTBytes;
  732. lh := GetTwoPTBytes;
  733. bc := GetTwoPTBytes;
  734. ec := GetTwoPTBytes;
  735. nw := GetTwoPTBytes;
  736. nh := GetTwoPTBytes;
  737. END; (* ReadTFMIntegers *)
  738.  
  739. (******************************************************************************)
  740.  
  741. PROCEDURE ReadTFMCharInfo;
  742.  
  743. (* Read the TFMinfo array.  See TFtoPL section 11. *)
  744.  
  745. VAR c, i : INTEGER;
  746.  
  747. BEGIN
  748. PToffset := 24 + (lh * 4);          (* offset of TFMinfo array *)
  749. FOR c := bc TO ec DO
  750.    WITH TFMinfo[c] DO BEGIN
  751.       wdindex  := GetPTByte * 4;    (* offset from start of width array *)
  752.       i        := GetPTByte;        (* 2nd byte contains htindex and dpindex *)
  753.       htindex  := (i DIV 16) * 4;   (* offset from start of height array *)
  754.       dpindex  := (i MOD 16) * 4;   (* offset from start of depth array *)
  755.       PToffset := PToffset + 2;     (* skip itindex and remainder bytes *)
  756.    END;
  757. END; (* ReadTFMCharInfo *)
  758.  
  759. (******************************************************************************)
  760.  
  761. PROCEDURE ReadTFMCharMetrics;
  762.  
  763. (* Read the charmetrics array using the indices in TFMinfo. *)
  764.  
  765. VAR wdbase, htbase, dpbase, b, c : INTEGER;
  766.  
  767. BEGIN
  768. wdbase := 24 + lh * 4 + (ec - bc + 1) * 4;   (* offset of width array *)
  769. htbase := wdbase + nw * 4;                   (* offset of height array *)
  770. dpbase := htbase + nh * 4;                   (* offset of depth array *)
  771. FOR c := bc TO ec DO
  772.    WITH TFMinfo[c] DO
  773.    WITH charmetrics[c] DO BEGIN
  774.       PToffset := wdbase + wdindex;
  775.       FOR b := 0 TO 3 DO width[b] := GetPTByte;
  776.       PToffset := htbase + htindex;
  777.       FOR b := 0 TO 3 DO height[b] := GetPTByte;
  778.       PToffset := dpbase + dpindex;
  779.       FOR b := 0 TO 3 DO depth[b] := GetPTByte;
  780.    END;
  781. END; (* ReadTFMCharMetrics *)
  782.  
  783. (******************************************************************************)
  784.  
  785. PROCEDURE TFMFillPixelTable;
  786.  
  787. (* Fill the pixeltable for currfont^ (a PostScript font)
  788.    using information in the currently open TFM file.
  789. *)
  790.  
  791. VAR c, dheight, pheight, ddepth, pdepth : INTEGER;
  792.  
  793. BEGIN
  794. ReadTFMIntegers;                         (* read lf..nh *)
  795. ReadTFMCharInfo;                         (* fill TFMinfo array *)
  796. ReadTFMCharMetrics;                      (* fill charmetrics array *)
  797. WITH currfont^ DO BEGIN
  798.    FOR c := 0 TO bc - 1 DO
  799.       pixelptr^[c].mapadr := 0;          (* chars < bc don't exist *)
  800.    FOR c := ec + 1 TO 255 DO
  801.       pixelptr^[c].mapadr := 0;          (* chars > ec don't exist *)
  802.    FOR c := bc TO ec DO
  803.       WITH pixelptr^[c] DO
  804.       WITH charmetrics[c] DO BEGIN
  805.          dwidth  := FixToDVI(width[0],width[1],width[2],width[3]);
  806.          dheight := FixToDVI(height[0],height[1],height[2],height[3]);
  807.          ddepth  := FixToDVI(depth[0],depth[1],depth[2],depth[3]);
  808.          (* convert DVI units to pixels *)
  809.          pwidth  := PixelRound(dwidth);
  810.          pheight := PixelRound(dheight);
  811.          pdepth  := PixelRound(ddepth);
  812.          (* Since we don't have access to bitmap info for a PostScript font
  813.             we will have to use the TFM width/height/depth info to
  814.             approximate wd, ht, xo, yo.
  815.          *)
  816.          wd := pwidth;
  817.          wd := wd - (wd DIV 8);          (* better approximation *)
  818.          ht := pheight + pdepth;
  819.          xo := 0;
  820.          yo := pheight - 1;
  821.          IF (wd = 0) OR (ht = 0) THEN
  822.             mapadr := 0                  (* char all-white or not in font *)
  823.          ELSE
  824.             mapadr := 1;                 (* anything but 0 *)
  825.          bitmap.mptr := NIL;
  826.       END;
  827. END;
  828. END; (* TFMFillPixelTable *)
  829.  
  830. (******************************************************************************)
  831.  
  832. PROCEDURE PixelTableRoutine;
  833.  
  834. (* DVIReader has just allocated a new pixeltable for currfont^ and
  835.    calls this routine from InterpretPage only ONCE per font
  836.    (the first time the font is used).
  837.    If this is the first time we've seen the font then we build fontspec first.
  838.    (Note that ShowStatistics in the main program may call BuildFontSpec first.)
  839.    If we can't open the font file we return dummyfont values, but using the
  840.    current font's scaledsize.
  841. *)
  842.  
  843. VAR ch : CHAR;
  844.  
  845. BEGIN
  846. WITH currfont^ DO BEGIN
  847.    IF fontspeclen = 0 THEN BuildFontSpec(currfont);
  848.    IF OpenFontFile(fontspec) THEN BEGIN
  849.       { DEBUG
  850.       ClearTextLine(messagel);
  851.       MoveToTextLine(messagel);
  852.       WriteString('Loading font data from'); WriteChar(' ');
  853.       WriteString(fontspec);
  854.       WriteLine;
  855.       GUBED }
  856.    END
  857.    ELSE IF OpenFontFile(dummyfont) THEN BEGIN
  858.       (* we will fill pixeltable with dummyfont values *)
  859.       ClearTextLine(messagel);
  860.       MoveToTextLine(messagel);
  861.       WriteString('Couldn''t open'); WriteChar(' '); WriteString(fontspec);
  862.       WriteString('!   Loading dummy font.');
  863.       WriteString('   RETURN:');
  864.       WriteBuffer;
  865.       REPEAT ReadChar(ch) UNTIL ch = CR;
  866.       ClearTextLine(messagel);
  867.       MoveToTextLine(messagel);
  868.       WriteBuffer;
  869.    END
  870.    ELSE BEGIN
  871.       ClearTextLine(messagel);
  872.       MoveToTextLine(messagel);
  873.       WriteString('Couldn''t open dummy font'); WriteChar(' ');
  874.       WriteString(dummyfont); WriteLine; RestoreTerminal; exit(1);
  875.    END;
  876.    IF psfont AND fontexists THEN
  877.       TFMFillPixelTable
  878.    ELSE
  879.       PKFillPixelTable;
  880.    CloseFontFile;
  881. END;
  882. END; (* PixelTableRoutine *)
  883.  
  884. (******************************************************************************)
  885.  
  886. PROCEDURE InitFontReader;
  887.  
  888. (* This routine initializes some global variables. *)
  889.  
  890. VAR i : INTEGER;
  891.  
  892. BEGIN
  893. gpower[0] := [];
  894. FOR i := 1 TO 32 DO gpower[i] := gpower[i-1] + [i-1];   (* used in GetBitmap *)
  895. psprefixlen := Len(psprefix);
  896. fontdirlen  := Len(fontdir);
  897. END; (* InitFontReader *)
  898.